﻿using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using Sripirom.ATS.Core.Logging;
using Sripirom.ATS.Core.Validator;

namespace Sripirom.ATS.Core.ServiceModel.Description
{
    /// <summary>
    /// Behavior defined for global exception handler
    /// </summary>
    public class AtsOperationBehavior : IOperationBehavior
    {
        private readonly ServiceCommandConfigElement _operationConfig;
        private readonly IParameterInspector _validatingParamterInspector;
        public AtsOperationBehavior(ServiceCommandConfigElement operationConfig)
        
        {
            LoggerFactory.CreateLog().Debug("I:Constructor AtsOperationBehavior " + operationConfig.Name);
            _operationConfig = operationConfig;
            var validators = new Sripirom.ATS.Core.Validator.IObjectValidator[]
            {
                new AtsHeaderValidatesObjectValidator(_operationConfig.MessageValidate.HeaderValidates),
                new AtsMessageValidatesObjectValidator(_operationConfig.MessageValidate.BodyValidates)
            };

            var errorMessageGenerator = new Sripirom.ATS.Core.Validator.ErrorMessageGenerator();

            _validatingParamterInspector = (IParameterInspector)System.Activator.CreateInstance(Type.GetType(_operationConfig.MessageValidate.Name)
                                                , validators, errorMessageGenerator);
                                                                                                                                                                                                                       
           LoggerFactory.CreateLog().Debug("O:Constructor AtsOperationBehavior " + operationConfig.Name);
           
        }


        public void Validate(OperationDescription operationDescription)
        {
            LoggerFactory.CreateLog().Debug("-AtsOperationBehavior.Validate");
            
            return;
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {// Here we have added our invoker to the dispatch operation 
            LoggerFactory.CreateLog().Debug("I:AtsOperationBehavior.ApplyDispatchBehavior " + operationDescription.Name);

            dispatchOperation.ParameterInspectors.Add(_validatingParamterInspector);

           dispatchOperation.Invoker =  (IOperationInvoker)System.Activator.CreateInstance(Type.GetType(_operationConfig.Name)
                                                , dispatchOperation.Invoker, dispatchOperation );

            LoggerFactory.CreateLog().Debug("O:AtsOperationBehavior.ApplyDispatchBehavior " + operationDescription.Name);
        }


        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
            LoggerFactory.CreateLog().LogInfo("-GlobalExceptionHandlerBehavior.ApplyClientBehavior");
            return;
        }


        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
            LoggerFactory.CreateLog().LogInfo("-GlobalExceptionHandlerBehavior.AddBindingParameters");
            
            return;
        }
    }
}